import requests
from lxml import etree

url = "https://tieba.baidu.com/t/f/?class=college"
req = requests.get(url)
tree = etree.HTML(req.text)
html = tree.xpath("/html")
body = html[0].xpath("./body")
#  <div class="each_topic_entrance_list">
div = body[0].xpath(".//div[@class='each_topic_entrance_list']")
for i in div:
    # <a class="each_topic_entrance_item" href="//tieba.baidu.com/t/f/91586" data-fid="91586"> 电子科技大学</a>
    a = i.xpath(".//a")
    for j in a:
        # print(j.xpath("./@data-fid"))
        # 拼接请求 https://tieba.baidu.com/t/f/31511
        url_data = "https://tieba.baidu.com/t/f/" + j.xpath("./@data-fid")[0]
        req_data = requests.get(url_data)
        tree_data = etree.HTML(req_data.text)
        html_data = tree_data.xpath("/html")
        body_data = html_data[0].xpath("./body")
        a_s = body_data[0].xpath(".//a[@class='thread_title']")

        try:
            for k in a_s:
                print(k.xpath("./text()")[0])
        except IndexError:
            pass


